//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//

#include <vector>
#include <map>
#include <iostream>

#include "inet/common/packet/serializer/ChunkSerializerRegistry.h"
#include "inet/transportlayer/udp/UdpHeader_m.h"
#include "RoceMsgSerializer.h"


namespace inet {
    using namespace std;

Register_Serializer(RoceMsg,RoceMsgSerializer);

void RoceMsgSerializer::serialize(inet::MemoryOutputStream& stream, const inet::Ptr<const inet::Chunk>& chunk) const
{
    // std::cout<<"serializeRoceMsgSerializer"<<endl;
    const auto& roceMsg = inet::staticPtrCast<const inet::RoceMsg>(chunk);
    //std::cout<<"compute_time"<<stream.getData().size()<<endl;
    for(int i = 0; i < (int)roceMsg->getThe_flow().data_size-1; ++i){
        stream.writeUint8((bool)false);
    }
    stream.writeUint8((bool)true);
    //std::cout<<"Bit length"<<stream.getData().size()-roceMsg->getThe_flow().data_size<<endl;

}

const inet::Ptr<inet::Chunk> RoceMsgSerializer::deserialize(inet::MemoryInputStream& stream) const
{
    // std::cout<<"deserializeRoceMsgSerializer"<<endl;
    auto roceMsg = inet::makeShared<RoceMsg>();
    int data_size = 0;
    bool isLastBit = false;
    while(!isLastBit){
        isLastBit =( bool)stream.readUint8();
        data_size++;
    }
 

    ai_sim::Flow flow;
    flow.data_size = data_size;
    roceMsg->setThe_flow(flow);
    return roceMsg;
 }

} // namespace inet
